<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="方案名称" prop="programmeName">
        <el-input
          v-model="queryParams.programmeName"
          placeholder="请输入方案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方案开始时间" prop="programmeStart">
        <el-date-picker clearable
          v-model="queryParams.programmeStart"
          type="date"
          :picker-options="startPickerOptions"
          value-format="yyyy-MM-dd"
          placeholder="请选择方案开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="方案结束时间" prop="programmeEnd">
        <el-date-picker clearable
          v-model="queryParams.programmeEnd"
          type="date"
          :picker-options="endPickerOptions"
          value-format="yyyy-MM-dd"
          placeholder="请选择方案结束时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="方案优先级" prop="programmePriority">-->
<!--        <el-input-->
<!--          v-model="queryParams.programmePriority"-->
<!--          placeholder="请输入方案优先级"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item v-hasPermi="['ticket:programme:query']">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ticket:programme:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ticket:programme:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ticket:programme:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ticket:programme:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="programmeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="方案id" align="center" prop="venuesProgrammeId" />-->
      <el-table-column label="方案名称" align="center" prop="programmeName" />
      <el-table-column label="方案开始时间" align="center" prop="programmeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.programmeStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="方案结束时间" align="center" prop="programmeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.programmeEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="方案优先级" align="center" prop="programmePriority" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ticket:programme:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ticket:programme:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场馆方案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-click-modal="dialogIsCloseOnModal" :close-on-press-escape="dialogIsCloseOnModal">
      <el-form ref="form" :model="form" :rules="rules" label-width="106px">
        <!-- 景区信息 -->
        <el-divider content-position="left">场馆信息</el-divider>
        <el-row>
          <el-col :span="24">
            <el-col :span="11">
              <el-form-item label="场馆" prop="ticOtherBaseId">
                  <el-select
                    v-model="form.ticOtherBaseId"
                    placeholder="请选择场馆"
                    clearable
                    style="width: 240px"
                    @change='changeScenicSelect'
                    :disabled='isEdit'
                  >
                    <el-option
                      v-for="dict in dictScenicList"
                      :key="dict.ticOtherId"
                      :label="dict.name"
                      :value="dict.ticOtherId"
                    />
                  </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <!-- 选择景区后展示详细信息 -->
          <el-col :span="24" v-if='form.ticOtherBaseId'>
            <el-col :span="11">
              <el-form-item label="星级" prop="starDetail">
                <el-input v-model="dictScenicListSelected.starDetail" disabled />
              </el-form-item>
<!--              <el-form-item label="" prop="sysStoresId" v-if="1==2">-->
<!--                <el-input v-model="dictScenicListSelected.sysStoresId" />-->
<!--              </el-form-item>-->
            </el-col>
            <el-col :span="12">
              <el-form-item label="开放时间" >
                <el-input v-if="dictScenicListSelected.openTimeStart" :value="`${dictScenicListSelected.openTimeStart}-${dictScenicListSelected.openTimeEnd}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="景点信息">
                <editor v-model="dictScenicListSelected.introduceDetails" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="游客须知">
                <editor v-model="dictScenicListSelected.touristNotice" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="地理位置" >
                <el-input v-model="dictScenicListSelected.addressDetails" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="坐标点" prop="starDetail">
                <el-input v-if="dictScenicListSelected.latitude &&dictScenicListSelected.longitude" :value="`${dictScenicListSelected.latitude}-${dictScenicListSelected.longitude}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
        <el-divider content-position="left">方案信息</el-divider>
        <el-row>
          <el-col :span="24">
            <el-col :span="11">
            <el-form-item label="方案名称" prop="programmeName" >
              <el-input v-model="form.programmeName" placeholder="请输入方案名称" style="width: 220px;"/>
            </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="11">
          <el-form-item label="方案开始时间" prop="programmeStart">
            <el-date-picker clearable
              v-model="form.programmeStart"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择方案开始时间">
            </el-date-picker>
          </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="方案结束时间" prop="programmeEnd">
            <el-date-picker clearable
              v-model="form.programmeEnd"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择方案结束时间">
            </el-date-picker>
          </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="方案优先级" prop="programmePriority">-->
<!--          <el-input v-model="form.programmePriority" placeholder="请输入方案优先级" />-->
<!--        </el-form-item>-->
        <el-divider content-position="left">方案时间段信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTicVenuesProgrammePeriod">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTicVenuesProgrammePeriod">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="ticVenuesProgrammePeriodList" :row-class-name="rowTicVenuesProgrammePeriodIndex" @selection-change="handleTicVenuesProgrammePeriodSelectionChange" ref="ticVenuesProgrammePeriod">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
<!--        -->
          <el-table-column label="开始时间" prop="periodTimeStart" >
            <template slot-scope="scope">
              <el-time-picker clearable
                style="width:180px"
                v-model="scope.row.periodTimeStart"
                format="HH:mm:ss"
                value-format="HH:mm:ss"
                :picker-options="{selectableRange:'00:00:00-23:59:59'}"
                placeholder="请选择开始时间">
              </el-time-picker>
            </template>
          </el-table-column>
          <el-table-column label="结束时间" prop="periodTimeEnd" >
            <template slot-scope="scope">
              <el-time-picker clearable
                style="width:180px"
                v-model="scope.row.periodTimeEnd"
                format="HH:mm:ss"
                value-format="HH:mm:ss"
                :picker-options="{selectableRange:  `${scope.row.periodTimeStart}-23:59:59`}"
                placeholder="请选择结束时间">
              </el-time-picker>
            </template>
          </el-table-column>
          <el-table-column label="价格" prop="periodPrice" width="210">
            <template slot-scope="scope">
              <el-input-number  v-model="scope.row.periodPrice" placeholder="请输入价格"  :min="0" :precision="2" controls-position="right"/>
            </template>
          </el-table-column>
          <el-table-column label="总库存" prop="periodStockCount" width="210">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.periodStockCount" placeholder="请输入库存" :min="0"  controls-position="right"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProgramme, getProgramme, delProgramme, addProgramme, updateProgramme,getListTicOherBase } from "@/api/ticket/programme";

export default {
  name: "Programme",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTicVenuesProgrammePeriod: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场馆方案表格数据
      programmeList: [],
      // 方案时间段表格数据
      ticVenuesProgrammePeriodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        programmeName: null,
        programmeStart: null,
        programmeEnd: null,
        programmePriority: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ticOtherBaseId:[
          { required: true, message: "请选择场馆",trigger: "change",},
        ],
        programmeName:[
          { required: true, message: "请输入方案名称",trigger: "blur",},
        ],
        programmeStart:[
          { required: true, message: "请选择方案开始时间",trigger: "change",},
        ],
        programmeEnd:[
          { required: true, message: "请选择方案结束时间",trigger: "change",},
        ],
      },
      //
      dictScenicList:[],
      dictScenicListSelected:[],
      isEdit:false,
      startPickerOptions: {
        disabledDate: (time) => {
          let endTime = this.queryParams.programmeEnd
          if (endTime) {
            return time.getTime() > new Date(endTime).getTime()
          }
        }
      },
      endPickerOptions: {
        disabledDate: (time) => {
          let startTime = this.queryParams.programmeStart
          if (startTime) {
            return time.getTime() < new Date(startTime).getTime()
          }
        }
      },
    };
  },
  created() {
    this.getList();
    this.getTicOherBase()
  },
  methods: {
    // 获取景点信息
    getTicOherBase(){
      getListTicOherBase().then(res=>{
        this.dictScenicList=res.data
      })
    },
    // 选择景点
    changeScenicSelect(selected){
      console.log('选择景点',this.dictScenicList,selected)
      this.form.ticOtherBaseId=selected
      this.dictScenicList.map(item=>{
        if(item.ticOtherId==selected){
          this.dictScenicListSelected=item
        }
      })
    },
    /** 查询场馆方案列表 */
    getList() {
      this.loading = true;
      listProgramme(this.queryParams).then(response => {
        this.programmeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        venuesProgrammeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ticOtherBaseId: null,
        programmeName: null,
        programmeType: null,
        programmeStart: null,
        programmeEnd: null,
        programmePriority: null
      };
      this.ticVenuesProgrammePeriodList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.venuesProgrammeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场馆方案";
      this.isEdit=false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isEdit=true
      this.reset();
      const venuesProgrammeId = row.venuesProgrammeId || this.ids
      getProgramme(venuesProgrammeId).then(response => {
        this.form = response.data;
        this.ticVenuesProgrammePeriodList = response.data.ticVenuesProgrammePeriodList;
        this.open = true;
        this.title = "修改场馆方案";
        this.changeScenicSelect(this.form.ticOtherBaseId)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.ticVenuesProgrammePeriodList.length<=0){
              this.$modal.msgError("请添加方案时间段信息后保存");
              return
          }
          for(var i=0;i<this.ticVenuesProgrammePeriodList.length;i++){
            let item=this.ticVenuesProgrammePeriodList[i]
            if(!item.periodTimeStart || !item.periodTimeEnd){
              this.$modal.msgError("请填写完整的方案时间段信息后保存");
              return
            }
          }
          this.form.ticVenuesProgrammePeriodList = this.ticVenuesProgrammePeriodList;
          this.form.sysStoresId=this.dictScenicListSelected.sysStoresId;
          if (this.form.venuesProgrammeId != null) {
            updateProgramme(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.sysStoresId=this.dictScenicListSelected.sysStoresId;
            addProgramme(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const venuesProgrammeIds = row.venuesProgrammeId || this.ids;
      this.$modal.confirm('是否确认删除场馆方案编号为"' + venuesProgrammeIds + '"的数据项？').then(function() {
        return delProgramme(venuesProgrammeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 方案时间段序号 */
    rowTicVenuesProgrammePeriodIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 方案时间段添加按钮操作 */
    handleAddTicVenuesProgrammePeriod() {
      let obj = {};
      obj.isDel = "";
      obj.ticOtherBaseId = "";
      obj.periodTimeStart = "";
      obj.periodTimeEnd = "";
      obj.periodPrice = "";
      obj.periodStockCount = "";
      this.ticVenuesProgrammePeriodList.push(obj);
    },
    /** 方案时间段删除按钮操作 */
    handleDeleteTicVenuesProgrammePeriod() {
      if (this.checkedTicVenuesProgrammePeriod.length == 0) {
        this.$modal.msgError("请先选择要删除的方案时间段数据");
      } else {
        const ticVenuesProgrammePeriodList = this.ticVenuesProgrammePeriodList;
        const checkedTicVenuesProgrammePeriod = this.checkedTicVenuesProgrammePeriod;
        this.ticVenuesProgrammePeriodList = ticVenuesProgrammePeriodList.filter(function(item) {
          return checkedTicVenuesProgrammePeriod.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTicVenuesProgrammePeriodSelectionChange(selection) {
      this.checkedTicVenuesProgrammePeriod = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ticket/programme/export', {
        ...this.queryParams
      }, `programme_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
